package modes

import (
	"fmt"
)

type LiveClass struct {
	Id           int64     `json:"live_id" xorm:"id"`                     //id
	PPId         int64     `json:"live_pid" xorm:"pid"`                   //项目id
	SSId         int64     `json:"live_sid" xorm:"sid"`                   //科目id
	TypeId       int64     `json:"live_type_id" xorm:"type_id"`           //直播课程类型
	Courseware   string    `json:"live_courseware" xorm:"courseware"`     //课件名称
	Descl        string    `json:"live_desc" xorm:"descl"`                //描述
	LAt          int64     `json:"live_lat" xorm:"lat"`                   //直播时间
	EAt          int64     `json:"live_eat" xorm:"eat"`                   //直播结束
	Clienturl    string    `json:"live_clienturl" xorm:"clienturl"`       //授课链接
	Assistanturl string    `json:"live_assistanturl" xorm:"assistanturl"` //助教链接
	ViewUrl      string    `json:"live_viewurl" xorm:"viewurl"`           //学员链接
	Instructor   string    `json:"live_instructor" xorm:"instructor"`     //授课老师
	States       int64     `json:"live_states" xorm:"states"`             // 状态 0 直播中 1 未开始  2 已结束
	SSName       []Subject `json:"s_name" xorm:"-"`                       //科目名称
}

func (this *LiveClass) TableName() string {
	return "liveclass"
}

func (this *LiveClass) Save() (int64, error) {
	return Db(1).Insert(this)
}

func (this *LiveClass) Get() (bool, error) {
	return Db(1).Get(this)
}
func (this *LiveClass) delete(where string) (int64, error) {
	return Db(1).Where(where).Delete(this)
}
func (this *LiveClass) Del() (int64, error) {
	where := fmt.Sprintf("id =%d", this.Id)
	return this.delete(where)
}
func (this *LiveClass) Count(where string) (int64, error) {
	var val LiveClass
	return Db(1).Where(where).Count(&val)
}
func (this *LiveClass) update(where string, field string) (int64, error) {
	return Db(1).Where(where).Cols(field).Update(this)
}

func (this *LiveClass) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *LiveClass) where(fage, count, page int, where, field string) ([]LiveClass, error) {
	list := make([]LiveClass, 0)
	var err error
	if field == "" {
		field = "id"
	}
	if 0 == fage { // 从小到大排序
		err = Db(1).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(1).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}

type LiveClassName struct {
	LiveClass `json:"li_class" xorm:"extends"`
	Project   `json:"proj" xorm:"extends"`
	LiveType  `json:"li_type" xorm:"extends"`
	LiveCid   `json:"cid" xorm:"extends"`
}

func (this *LiveClassName) Lookups(cidlist []int64, val *Pager) error {
	list := make([]LiveClassName, 0)
	val.TName = "liveclass"
	//fmt.Println("cidlist",cidlist)
	val.Page--
	var field, sort string
	switch val.Coll {
	case 0:
		field = fmt.Sprintf("%s.id", val.TName) // id
	case 1:
		field = fmt.Sprintf("%s.pid", val.TName) // 项目
	case 2:
		field = fmt.Sprintf("%s.sid", val.TName) // 科目
	case 3:
		field = fmt.Sprintf("%s.lat", val.TName) //状态 0 直播中 1 未开始  2 已结束
	default:
		field = fmt.Sprintf("%s.lat", val.TName) // id
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}
	//swhere := fmt.Sprintln("cid in ()" ,cidlist)
	val.Total, _ = Db(1).Table(val.TName).Where(val.ToWhere()).Count(this)
	err := Db(1).Table(val.TName).
		Join("LEFT", "project", fmt.Sprintf("%s.pid = project.id", val.TName)).
		Join("LEFT", "livetype", fmt.Sprintf("%s.type_id = livetype.id", val.TName)).
		Join("LEFT", "livecid", fmt.Sprintf("%s.id = livecid.live_id", val.TName)).
		In("livecid.cid", cidlist).
		Where(val.ToWhere()).
		//Where(swhere).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
		for i, row := range list {
			list[i].SSName, _ = GetSname(row.SSId)
		}
		val.List = list
	}
	fmt.Println(list)
	return err
}
